var app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        entrust_bid : "",         // 委托出价数据

        auction_id:"",            // 活动id
        goods_id:"",              // 商品id

        price:0,                  // 委托出价金额
    },
    // 设置委托出价金额
    setPrice(e){
        var price = parseFloat(e.detail.value);
        this.setData({price})   
    },
    // 提交订单
    formSubmit(e){
        var formID = e.detail.formId;

        var self = this;
        var price = this.data.price;
        var auction_id = this.data.auction_id;
        var goods_id = this.data.goods_id;

        var url = "/entrust/entrust_bid";


        if (!price){
            wx.showToast({
                image:"../../img/warning.png",
                title: '请输入出价金额',
            })
            return false;
        }
        if (price < this.data.entrust_bid.price){
            wx.showToast({
                image: "../../img/warning.png",
                title: '出价金额过低',
            })
            return false;
        }

        price += this.data.entrust_bid.margin;

        wx.getStorage({
            key: 'user',
            success: function (res) {
                wx.request({
                    url: app.url.requestUrl + url,
                    method: 'POST',
                    data: {
                        openid: res.data.openid,
                        token: res.data.token,
                        money:price,
                        goods_id,
                        auction_id
                    },
                    success: function (res) {
                        var list = res.data;
                        if (res.data.code == 1) {
                            wx.navigateTo({
                                url: `../pay-style/pay-style?money=${list.data.money}&order_no=${list.data.trade_no}&order_type=entrust_order`,
                            })
                        }else{
                            wx.showModal({
                                title: '温馨提示',
                                content: res.data.msg,
                            })
                        }
                    }
                })
            },
        })
    },
    // 获取委托数据
    getEntrustPrice(id){
        var self = this;
        var url = "/entrust/entrust_bid_details";
        var goods_id = id; 

        wx.getStorage({
            key: 'user',
            success: function (res) {
                wx.request({
                    url: app.url.requestUrl + url,
                    method: 'POST',
                    data: {
                        openid: res.data.openid,
                        token: res.data.token,
                        goods_id
                    },
                    success: function (res) {
                        if (res.data.code == 1) {
                            self.setData({
                                entrust_bid: res.data.data,
                            })
                        }
                    }
                })
            },
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        console.log(options)
        this.setData({
            auction_id: options.auction_id,
            goods_id: options.id
        })
        this.getEntrustPrice(options.id)
    }
})